package site.shader85.OICQBind;


import java.util.Properties;
import java.util.Random;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


import static site.shader85.OICQBind.BindPlugin.stmp;

public class Utils {
	

	
	public static int send(String id){
		int uid = random();
		final Properties props = new Properties();
		props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.host", stmp.getString("host"));
        props.put("mail.user", stmp.getString("user"));
        props.put("mail.password", stmp.getString("password"));
        Authenticator authenticator = new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                String userName = props.getProperty("mail.user");
                String password = props.getProperty("mail.password");
                return new PasswordAuthentication(userName, password);
            }
        };
        Session mailSession = Session.getInstance(props, authenticator);
        MimeMessage message = new MimeMessage(mailSession);
        try{
        InternetAddress form = new InternetAddress(props.getProperty("mail.user"));
        message.setFrom(form);
        InternetAddress to = new InternetAddress(id+"@qq.com");
        message.setRecipient(RecipientType.TO, to);
        message.setSubject("[服务器娘]接收你的验证码!");
        message.setContent("<a href=''>Your Code is: "+uid+" </a>", "text/html;charset=UTF-8");
        Transport.send(message);}
        catch(Exception e){System.err.println("邮件发送失败，请检查您的配置是否正确");}
        return uid;
	}
	
	public static void print(Object obj){
		System.out.println(obj);
	}
	
	public static int random(){
		Random rd = new Random();
		 int tmp = rd.nextInt(1000000);
		 if(tmp>=100000)
			 return tmp;
		 else
			 return random();
	}
	
	public static boolean isQQ(String qq){
		if(qq.length()<6|qq.length()>12)
			return false;
		return qq.matches("\\d+");
	}
}
